<template>
  <div>
    <span
      :class="isFullscreen?'exit-fullscreen':'fullscreen'"
      @click="click"
    >
      <i
        :class="isFullscreen?'icon-quitquanping':'icon-quanping'"
        class="iconfont"
      />
    </span>
  </div>
</template>

<script>
import screenfull from 'screenfull';
/**
 * 切换全屏
 * @author maolin
 */
export default {
  name: 'Screenfull',
  data () {
    return {
      isFullscreen: false
    };
  },
  mounted () {
    this.init();
  },
  beforeDestroy () {
    this.destroy();
  },
  methods: {
    /**
     * 切换全屏
     * @public
     */
    click () {
      if (!screenfull.enabled) {
        this.$message({
          message: 'you browser can not work',
          type: 'warning'
        });
        return false;
      }
      screenfull.toggle();
    },
    /**
     *
     * @private
     */
    change () {
      this.isFullscreen = screenfull.isFullscreen;
    },
    /**
     *
     * @private
     */
    init () {
      if (screenfull.enabled) {
        screenfull.on('change', this.change);
      }
    },
    /**
     *
     * @private
     */
    destroy () {
      if (screenfull.enabled) {
        screenfull.off('change', this.change);
      }
    }
  }
};
</script>

<style scoped>
.screenfull-svg {
  display: inline-block;
  cursor: pointer;
  fill: #5a5e66;;
  width: 20px;
  height: 20px;
  vertical-align: 10px;
}
</style>
